# -*- coding: utf-8 -*-
import scrapy
import re
from paper.items import PaperItem


class SevenDaysWeatherSpider(scrapy.Spider):
    name = 'seven_days_weather'
    allowed_domains = ['forecast.weather.com.cn']
    start_urls = ['http://forecast.weather.com.cn/town/weathern/101080708001.shtml',
                  'http://forecast.weather.com.cn/town/weathern/101080708002.shtml#input',
                  'http://forecast.weather.com.cn/town/weathern/101080708003.shtml#input',
                  'http://forecast.weather.com.cn/town/weathern/101080708004.shtml#input',
                  'http://forecast.weather.com.cn/town/weathern/101080708005.shtml#input',
                  'http://forecast.weather.com.cn/town/weathern/101080708006.shtml#input',
                  'http://forecast.weather.com.cn/town/weathern/101080708007.shtml#input',
                  'http://forecast.weather.com.cn/town/weathern/101080708008.shtml#input']

    def parse(self, response):
        item = PaperItem()
        # 日期列表
        rs = response.xpath(
            "/html/body/div[@class='L_weather']/div[@class='fl weather_left']/div[@class='weather_7d']/div[@class='blueFor-container']/ul[@class='date-container']")
        # 天气情况列表
        rw = response.xpath(
            "/html/body/div[@class='L_weather']/div[@class='fl weather_left']/div[@class='weather_7d']/div[@class='blueFor-container']/ul[@class='blue-container backccc']")
        # 天气
        which_day = rs.xpath(".//li[@class='date-item']/p[@class='date']/text()").extract()
        # 昨日天气
        start_which_day = rs.xpath("//li[@class='date-item active']/p[@class='date']/text()").extract_first()
        # 最后天气
        end_which_day = rs.xpath("//li[@class='date-item last-item']/p[@class='date']/text()").extract_first()
        result_which_list = which_day + [start_which_day] + [end_which_day]
        sort_which_day = []
        # 排序、匹配数字
        if len(result_which_list) > 0:
            for sort_l in result_which_list:
                sort_which_day.append(re.findall('\d+', sort_l)[0])
        sort_which_day.sort()
        item['which_day'] = sort_which_day[1:]
        """
            初始化的值
        """
        # 天气状况
        weather_w = []
        # 风向
        weather_wind = {'one': [], 'two': []}
        # 风力
        weather_wind_dir = []

        weather = rw.xpath(".//li[@class='blue-item']")
        """
            昨日
        """
        # 昨日天气
        old_weather = rw.xpath(".//li[@class='blue-item yesterday']/p[1]/text()").extract_first()
        # 昨日 - 风力
        old_wind = rw.xpath(".//li[@class='blue-item yesterday']/p[4]/text()").extract_first()
        old_wind = old_wind.strip()
        weather_wind_dir.append(old_wind)
        # 昨日 - 风向
        ol_dir_one = rw.xpath(
            ".//li[@class='blue-item yesterday']/div[@class='wind-container']/i[1]/@title").extract_first()
        ol_dir_two = rw.xpath(
            ".//li[@class='blue-item yesterday']/div[@class='wind-container']/i[2]/@title").extract_first()
        weather_wind['one'].append(ol_dir_one)
        weather_wind['two'].append(ol_dir_two)
        """
            当天
        """
        # 当天的天气
        in_the_day = rw.xpath(".//li[@class='blue-item active']/p[1]/text()").extract_first()
        in_the_day = self.format_chin(in_the_day)
        # 当天 - 风力
        in_the_day_win = rw.xpath(".//li[@class='blue-item active']/p[@class='wind-info']/text()").extract_first()
        in_the_day_win = in_the_day_win.strip()
        weather_wind_dir.append(in_the_day_win)
        # 当天 - 风向
        in_the_day_dir_one = rw.xpath(
            ".//li[@class='blue-item active']/div[@class='wind-container']/i[1]/@title").extract_first()
        in_the_day_dir_two = rw.xpath(
            ".//li[@class='blue-item active']/div[@class='wind-container']/i[2]/@title").extract_first()
        weather_wind['one'].append(in_the_day_dir_one)
        weather_wind['two'].append(in_the_day_dir_two)
        """
            第七天
        """
        # 第七天天气
        seven_weather = rw.xpath(".//li[@class='blue-item blue-item-last']/p[1]/text()").extract_first()
        old_weather = self.format_chin(old_weather)
        seven_weather = self.format_chin(seven_weather)
        # 第七天 - 风力
        seven_weather_win = rw.xpath(
            ".//li[@class='blue-item blue-item-last']/p[@class='wind-info']/text()").extract_first()
        seven_weather_win = seven_weather_win.strip()
        # 第七天 - 风向
        seven_weather_dir_one = rw.xpath(
            ".//li[@class='blue-item blue-item-last']/div[@class='wind-container']/i[1]/@title").extract_first()
        seven_weather_dir_two = rw.xpath(
            ".//li[@class='blue-item blue-item-last']/div[@class='wind-container']/i[2]/@title").extract_first()
        weather_w.append(old_weather)
        weather_w.append(in_the_day)
        for value in weather:
            wea_value = value.xpath('.//p[@class="weather-info"]/text()').extract_first()
            weather_match = re.findall(r'[\u4e00-\u9fa5]', wea_value)
            weather_w_item = ''.join(weather_match)
            # 其他 - 风力
            weather_w_wind = value.xpath(
                ".//p[@class='wind-info']/text()").extract_first()
            weather_w_wind = weather_w_wind.strip()
            weather_wind_dir.append(weather_w_wind)
            weather_w.append(weather_w_item)
            # 其他 - 风向
            other_weather_dir_one = value.xpath(".//div[@class='wind-container']/i[1]/@title").extract_first()
            other_weather_dir_two = value.xpath(".//div[@class='wind-container']/i[2]/@title").extract_first()
            weather_wind['one'].append(other_weather_dir_one)
            weather_wind['two'].append(other_weather_dir_two)

        weather_w.append(seven_weather)
        weather_wind_dir.append(seven_weather_win)
        weather_wind['one'].append(seven_weather_dir_one)
        weather_wind['two'].append(seven_weather_dir_two)

        # 白天气温
        result = response.xpath(
            "/html/body/div[@class='L_weather']/div[@class='fl weather_left']/div[@class='weather_7d']/div[@class='blueFor-container']/script").extract_first()
        eventDay = re.search('eventDay', result).span()
        eventDayStart = result[eventDay[1]:]
        closeEvent = re.search(']', eventDayStart).span()
        # 最终结果 - 白天气温
        eventDayResult = eval(eventDayStart[2:closeEvent[1]].strip())
        # 晚上气温
        eventNight = re.search('eventNight', result).span()
        eventNightStart = result[eventNight[1]:]
        eventNightClose = re.search(']', eventNightStart).span()
        # 最终结果 - 白天气温
        eventNightResult = eval(eventNightStart[2:eventNightClose[1]].strip())

        # # 当前页面的乡镇名称
        city_x = response.xpath(
            "/html/body/div[@class='weather_location']/div[@class='areaSelect webox'][3]/a/text()").extract_first()
        item['city_name'] = city_x
        """
            数据处理
        """
        # 天气
        item['weather'] = weather_w[1:]
        # 最高气温
        item['max_temperature'] = eventDayResult[1:]
        # 最低气温
        item['min_temperature'] = eventNightResult[1:]
        # 最高风速
        item['max_wind'] = weather_wind_dir[1:]
        # 开始风向
        item['dir_start'] = weather_wind['one']
        # 结束风向
        item['dir_end'] = weather_wind['two']
        # 状态值
        item['status'] = 2
        yield item
        pass

    """
        格式为中文字符
    """

    def format_chin(self, value):
        result = re.findall(r'[\u4e00-\u9fa5]', value)
        if len(result) > 0:
            return ''.join(result)
        return ''

    """ 
        格式为数字
    """

    def format_num(self, value):
        result = re.match(r'(.*) are (.*?) .*', value)
        if len(result) > 0:
            return ''.join(result)
        return ''
